package com.yangaobo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yangaobo.dto.ClubProgressInsertDTO;
import com.yangaobo.dto.ClubProgressPageDTO;
import com.yangaobo.dto.ClubProgressUpdateDTO;
import com.yangaobo.entity.ClubProgress;
import com.yangaobo.excel.ClubProgressExcel;
import com.yangaobo.exception.IllegalParamException;
import com.yangaobo.exception.ServerErrorException;
import com.yangaobo.mapper.ClubProgressMapper;
import com.yangaobo.service.ClubProgressService;
import jakarta.annotation.Resource;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 杨奥博
 */
@Service
@CacheConfig(cacheNames = "clubProgress")
public class ClubProgressServiceImpl implements ClubProgressService {

    @Resource
    private ClubProgressMapper clubProgressMapper;

    @CacheEvict(allEntries = true)
    @Override
    public int insert(ClubProgressInsertDTO dto) {
        String info = dto.getInfo();
        // 拷贝属性
        ClubProgress clubProgress = BeanUtil.copyProperties(dto, ClubProgress.class);
        // 设置默认值
        clubProgress.setInfo(StrUtil.isBlank(info) ? "暂无描述" : info);
        clubProgress.setVersion(0L);
        clubProgress.setDeleted(0);
        clubProgress.setCreated(LocalDateTime.now());
        clubProgress.setUpdated(LocalDateTime.now());
        // DB添加
        int result = clubProgressMapper.insert(clubProgress);
        if (result <= 0) {
            throw new ServerErrorException("DB添加失败");
        }
        return result;
    }

    @Cacheable(key = "#p0", condition = "#p0 != null", unless = "#result == null")
    @Override
    public ClubProgress select(Long id) {
        ClubProgress result = clubProgressMapper.select(id);
        if (ObjectUtil.isNull(result)) {
            throw new ServerErrorException("DB查询失败");
        }
        return result;
    }

    @Cacheable(key = "#root.methodName", condition = "#p0 != null", unless = "#result == null")
    @Override
    public List<ClubProgress> list() {
        return clubProgressMapper.list(new ClubProgressPageDTO());
    }

    @Cacheable(key = "#root.methodName + ':' + #p0.toString()", condition = "#p0 != null", unless = "#result == null")
    @Override
    public PageInfo<ClubProgress> page(ClubProgressPageDTO dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        return new PageInfo<>(clubProgressMapper.list(dto));
    }

    @CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int update(ClubProgressUpdateDTO dto) {
        ClubProgress clubProgress = clubProgressMapper.select(dto.getId());
        if (ObjectUtil.isNull(clubProgress)) {
            throw new ServerErrorException("DB查询失败");
        }
        BeanUtil.copyProperties(dto, clubProgress);
        // 设置默认值
        clubProgress.setUpdated(LocalDateTime.now());
        // DB修改
        int result = clubProgressMapper.update(clubProgress);
        if (result <= 0) {
            throw new ServerErrorException("DB修改失败");
        }
        return result;
    }

    @CacheEvict(allEntries = true)
    @Override
    public int delete(Long id) {
        int result = clubProgressMapper.delete(id);
        if (result <= 0) {
            throw new ServerErrorException("DB删除失败");
        }
        return result;
    }

    @CacheEvict(allEntries = true)
    @Override
    public int deleteBatch(List<Long> ids) {
        int result = clubProgressMapper.deleteBatch(ids);
        if (result <= 0) {
            throw new ServerErrorException("DB删除失败");
        }
        return result;
    }

    @Override
    public List<ClubProgressExcel> getExcelData() {
        return clubProgressMapper.list(new ClubProgressPageDTO())
                .stream()
                .map(clubProgress -> {
                    ClubProgressExcel clubProgressExcel = BeanUtil.copyProperties(clubProgress, ClubProgressExcel.class);
                    if (ObjectUtil.isNotNull(clubProgress.getClub())) {
                        clubProgressExcel.setClubTitle(clubProgress.getClub().getTitle());
                    }
                    if (ObjectUtil.isNotNull(clubProgress.getRoom())) {
                        clubProgressExcel.setRoomTitle(clubProgress.getRoom().getTitle());
                    }
                    if (ObjectUtil.isNotNull(clubProgress.getCourse())) {
                        clubProgressExcel.setCourseTitle(clubProgress.getCourse().getTitle());
                    }
                    if (ObjectUtil.isNotNull(clubProgress.getManager())) {
                        clubProgressExcel.setManagerName(clubProgress.getManager().getRealname());
                    }
                    if (ObjectUtil.isNotNull(clubProgress.getAssistant())) {
                        clubProgressExcel.setAssistantName(clubProgress.getAssistant().getRealname());
                    }
                    if (ObjectUtil.isNotNull(clubProgress.getTeacher())) {
                        clubProgressExcel.setTeacherName(clubProgress.getTeacher().getRealname());
                    }
                    return clubProgressExcel;
                })
                .collect(Collectors.toList());
    }

    @CacheEvict(allEntries = true)
    @Override
    public int endClubProgress(Long id) {
        // 查询班级进度记录
        ClubProgress clubProgress = clubProgressMapper.select(id);
        if (ObjectUtil.isNull(clubProgress)) {
            throw new ServerErrorException("班级进度记录不存在");
        }
        // 判断是否已结束
        if (ObjectUtil.isNotNull(clubProgress.getEndTime())) {
            throw new IllegalParamException("班级进度已结束");
        }
        // DB修改：修改班级进度记录
        clubProgress.setEndTime(LocalDateTime.now());
        int result = clubProgressMapper.update(clubProgress);
        if (result <= 0) {
            throw new ServerErrorException("DB班级进度记录修改失败");
        }
        return result;
    }

}
